package client.comm.baoding.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.*
import client.comm.baoding.api.bean.SignData
import client.comm.baoding.databinding.ActivityPaiduiBinding
import client.comm.baoding.databinding.ActivityWdgpBinding
import client.comm.baoding.ui.vm.PaiduiViewModel
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.comm_ui.draw.SpaceItemDecoration
import client.comm.commlib.dialog.ValidatePwdDialog
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.toDp
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.flow.*

class PaiduiActivity : BaseMiddleActivity<PaiduiViewModel, ActivityPaiduiBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_paidui
    }

    val adapter: SignAdapter by lazy {
        SignAdapter().apply {

            refreshLoading = {
                if (binding.emptyLayout.visibility == View.VISIBLE) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
//                if (itemCount == 0) {
//                    binding.emptyLayout.errorType = EmptyLayout.NODATA
//                } else {
//                }
                binding.emptyLayout.visibility = View.GONE
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }

        }
    }

    val headerAdapter: PaiduiHeaderAdapter by lazy {
        PaiduiHeaderAdapter(viewModel, this)
    }

    override fun initView() {
        binding?.apply {
            event = this@PaiduiActivity
            vm = viewModel

        }
        binding.recyclerView.adapter = adapter.withLoadStateHeaderAndFooter(
            header = headerAdapter,
            footer = PagerFooterAdapter()
        )
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
//
        viewModel.orderId.value = intent.getStringExtra("orderId")

        val canSign = intent.getIntExtra("canSign", 0)
        val nowMoney = intent.getStringExtra("nowMoney")
        viewModel.btnEnable.value = canSign > 0
        if (canSign == 1) {
            viewModel.btnTxt.value = "签到"
        } else if (canSign == 0) {
            viewModel.btnTxt.value = "暂不能签到"
        }
        if (nowMoney?.toDouble() == 0.0) {
            viewModel.btnTxt.value = "结束"
            viewModel.btnEnable.value = false
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launchWhenResumed {
            viewModel.pagingDataAuto {
                val ret = viewModel.server.sign_order_record(it, viewModel.orderId.value)
                PagingJsonResult(
                    list = ret.body()?.data?.sign_list,
//                    list = arrayListOf(SignData.Order("")),
                    code = ret.body()?.code,
                    message = ret.body()?.message,
                    errorStr = ret.errorBody()?.string()
                )
            }.collect {
                adapter.submitData(it)
            }
        }
    }

    override fun observerUI() {
        viewModel.signRet.observe(this, Observer {
            it?.let {
                if (it.isSuccess) {
                    toast("签到成功")
                    viewModel.btnTxt.value = "暂不能签到"
                    viewModel.btnEnable.value = false
                    headerAdapter.notifyDataSetChanged()
                    adapter.refresh()
                } else {
                    toast(it.message)
                }
            }
        })

        viewModel.isShowing.observe(this, {
            viewModel.btnShowEnable.value = !viewModel.isShowing.value!! && it
        })
        viewModel.btnEnable.observe(this, {
            viewModel.btnShowEnable.value = !viewModel.isShowing.value!! && it
        })

    }

    override fun initViewModel(): PaiduiViewModel = createViewModel()
}